﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FySystem.Extension
{
    public static class ListExtension
    {
        /// <summary>
        /// 找出匹配的索引位置
        /// </summary>
        /// <param name="source"></param>
        /// <param name="select"></param>
        /// <returns></returns>
        public static int IndexOf(this List<byte> source, int startIndex,byte[] select)
        {
            int res = -1;
            try
            {
                if (source == null)
                    return res;
                if (select == null)
                    return res;
                if (source.Count == 0 && select.Length == 0)
                    return 0;
                if (startIndex > source.Count - 1)
                    return res;

                int matchIndex = 0;
                int beginIndex = -1;
                startIndex = startIndex < 0 ? 0 : startIndex;

                for(int i = startIndex; i < source.Count; i++)
                {
                    if (source[i] == select[matchIndex])
                    {
                        //如果相同
                        if (matchIndex == select.Length - 1)
                        {
                            //说明完成了所有字节的比较
                            res = beginIndex;
                            break;
                        }
                        else if (matchIndex == 0)
                        {
                            //说明是第一次匹配
                            beginIndex = i;
                        }
                        matchIndex++;
                    }
                    else
                    {
                        //如果不同
                        matchIndex = 0;
                        beginIndex = -1;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        public static bool IsNullOrEmpty<T>(this List<T> source)
        {
            if (source == null)
                return true;
            if (source.Count == 0)
                return true;
            return false;
        }

        public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
        {
            if (source == null)
                return true;
            if (source.Count() == 0)
                return true;
            return false;
        }

        /// <summary>
        /// 转换为sql的in查询条件
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="quotes">是否需要单引号</param>
        /// <returns></returns>
        public static string ToSqlInCondition<T>(this List<T> arr, bool quotes)
        {
            StringBuilder res = new StringBuilder();
            try
            {
                if (arr.IsNullOrEmpty())
                    return "";

                for (int i = 0; i < arr.Count; i++)
                {
                    if (i > 0)
                        res.Append(',');
                    if (quotes)
                        res.Append($"'{arr[i]}'");
                    else
                        res.Append(arr[i]);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res.ToString();
        }

        /// <summary>
        /// 转换为sql的in查询条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arr"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="quotes"></param>
        /// <returns></returns>
        public static string ToSqlInCondition<T>(this List<T> arr, int startIndex, int endIndex, bool quotes)
        {
            StringBuilder res = new StringBuilder();
            try
            {
                if (arr.IsNullOrEmpty())
                    return "";

                if (endIndex > arr.Count - 1)
                    endIndex = arr.Count - 1;

                for (int i = startIndex; i <= endIndex; i++)
                {
                    if (i > startIndex)
                        res.Append(',');
                    if (quotes)
                        res.Append($"'{arr[i]}'");
                    else
                        res.Append(arr[i]);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res.ToString();
        }
    }
}
